layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;

	var table_filter = 'audit_list',
		form_filter = 'audit_search_form';
	var where_json = {
		_o: 'audit_id',
		_b: 'desc'
	};
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: audit_list_url,
		method: 'post',
		height: 'full-200',
		cols: [audit_cols],
		where:{
			audit_state:1	
		},
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 30,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: '#auditToolbar',
		defaultToolbar: [],
		initSort: {
			field: 'audit_id',
			type: 'desc'
		}
	});
	
	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		var checkStatus = table.checkStatus(table_filter);
		if (checkStatus.data.length == 0) {
			layer.msg('请选择商品', {
				icon: 2,
				anim: 6
			});
			return false;
		}
		audit_id_arr = new Array();
		for (var i in checkStatus.data) {
			if(checkStatus.data[i].audit_state != 1){
				layer.msg('存在已审核的商品', {
					icon: 2,
					anim: 6
				});
				return false;
			}
			audit_id_arr.push(checkStatus.data[i].audit_id);
		}
		switch (obj.event) {
			case 'allPass':
				layer.confirm('确定审核通过选中商品吗？', {
					yes: function() {
						layer.load(2);
						send_info(audit_all_pass_url, {audit_id_arr:audit_id_arr}, function(data) {
							layer.closeAll();
							layer.msg(data.msg, {
								icon: data.code,
								anim: 6
							});
							table.reload(table_filter);
						})
					}
				});
				break;
			case 'allReject':
				layer.prompt({
					'title':'请输入拒绝原因'
				},function(value,index,elem){
					value = value.trim();
					if(value == ''){
						layer.msg('请输入拒绝原因');
						return false;
					}
					layer.load(2);
					send_info(autid_all_refuse_url, {
						audit_id_arr: audit_id_arr,
						remark:value
					}, function(data) {
						layer.closeAll();
						layer.msg(data.msg, {
							icon: data.code,
							anim: 6
						});
						table.reload(table_filter);
					})
				})
				break;
			default:
				break;
		}
		return false;
	})

	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	form.render();
	// 表单input 查询
		$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	})
	form.on('radio', function(data) {
		var value = data.value;
		var e = data.elem;
		var name = $(e).attr('name');
		select_search_list(name, value);
	})
	var select_search_list = function(field, value) {
		console.log(field, value);
		var where = {};
		where[field] = value;
		where_json[field] = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	};
	var create_time_start = laydate.render({
		elem: 'input[name="audit_form_create_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search_list('create_time_start', value);
		}
	});
	var create_time_end = laydate.render({
		elem: 'input[name="audit_form_create_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search_list('create_time_end', value);
		}
	});
	// select 查询
	form.on('select(site_id)', function(data) {
		var value = data.value;
		var where = {};
		where['site_id'] = value;
		table.reload(table_filter, {
			where: where,
			page:{
				curr:1
			}
		});
	})
	form.on('select(audit_method)', function(data) {
		var value = data.value;
		var where = {};
		where['audit_method'] = value;
		table.reload(table_filter, {
			where: where
		});
	})
	form.on('radio',function(data){
		var value = data.value;
		var where = {};
		var e = data.elem;
		where[$(e).attr('name')] = value;
		table.reload(table_filter, {
			where: where,
			page:{
				curr:1
			}
		});
	})

	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var audit_id = data.audit_id;
		console.log(data);
		switch(e) {
			case 'view':
				layer.open({
					type: 2,
					anim: -1,
					title: '查看',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: audit_view_url+'?audit_id='+audit_id
				})
				break;
			case 'refuse':
				layer.prompt({
					'title':'请输入拒绝原因'
				},function(value,index,elem){
					value = value.trim();
					if(value == ''){
						layer.msg('请输入拒绝原因');
						return false;
					}
					send_info(audit_refuse_url, {
						audit_id: audit_id,
						remark:value
					}, function(data) {
						layer.closeAll();
						if(data.code == 0) {
							layer.msg('操作成功', {
								icon: 1
							});
							table.reload(table_filter);
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				return false;
				layer.confirm('确定拒绝申请吗？', function(index) {
					send_info(audit_refuse_url, {
						audit_id: audit_id
					}, function(data) {
						if(data.code == 0) {
							layer.msg('操作成功', {
								icon: 1
							});
							table.reload(table_filter);
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				});
				break;
			case 'pass':
				layer.confirm('确定审核通过吗？', function(index) {
					send_info(audit_pass_url, {
						audit_id: audit_id
					}, function(data) {
						if(data.code == 0) {
							layer.msg('操作成功', {
								icon: 1
							});
							table.reload(table_filter);
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				});
				break;
			case 'del':
				// 删除
				layer.confirm('确定删除吗？', function(index) {

					send_info(site_delete_url, {
						site_id: site_id
					}, function(data) {
						if(data.code == 0) {
							obj.del();
							layer.msg('删除成功', {
								icon: 1
							});
							// 重载回收站
							if($('table[lay-filter="recycle_site_list"]').length > 0){
								table.reload('recycle_site_list');
							}
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			default:
				break;
		}
	})
})